<?php
/**
 * Created by PhpStorm.
 * User: jungshen
 * Date: 2018/8/2
 * Time: 14:45
 */

namespace app\api\controller\information;

use app\api\controller\BasicUserApi;
use Db;
use think\facade\Request;


class Dynamic extends BasicUserApi
{
    /**
     * @Notes: 发布动态
     * @author: Forska
     * @email: 736523388@qq.com
     * @DateTime: 2018/10/17 11:08
     */
    public function push(){
       $content = $_POST['content'];
       $images = $_POST['images'];
       if(empty($content)){
           return json(['msg'=>'参数错误'],403);
       }
       $data = [
           'mid'=>UID,
           'content'=>$content,
           'images'=>$images,
           'province'=>input('province',0),
           'create_at'=>time(),
           'status'=>0
       ];
       $re = Db::table('dynamic')->insert($data);
       if($re){
           log_credit_change(UID,RELEASE_INFORMATION_CREDIT,RELEASE_INFORMATION,'发布动态加信用积分');
           return json(['msg'=>'操作成功'],200);
       }else{
           return json(['msg'=>'网络错误'],500);
       }

    }

    /**
     * @Notes: 删除动态
     * @author: Forska
     * @email: 736523388@qq.com
     * @DateTime: 2018/10/17 11:14
     */
    public function del(){
        $id = $_POST['id'];
        $map = [
            'mid'=>UID,
            'id'=>array('eq',$id)
        ];
        $info = Db::table('dynamic')->where($map)->find();
        if(empty($info)){
            return json(['msg'=>'禁止访问，服务器收到请求，但是拒绝提供服务'],403);
        }
        $re = Db::table('dynamic')->delete($id);
        if($re){
            return json(['msg'=>'操作成功'],200);
        }else{
            return json(['msg'=>'网络错误'],500);
        }
    }
    public function lists(){
        $list = Db::table('dynamic')->where('mid',UID)->order('create_at desc')->select();
        foreach ($list as &$item) {
            $item['images'] = explode('|',$item['images']);
            $item['create_at'] = date('Y-m-d',$item['create_at']);
        }
        return json(['msg'=>'success','data'=>$list],200);
    }

}